package com.tma.internship.activities;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.tma.internship.connectserver.ContactServer;
import com.tma.internship.connectserver.ServerInfo;
import com.tma.internship.connectserver.ServiceCallback;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class DownloadFileActivity extends Activity implements OnClickListener{
	private Button controlBtn;
	private TextView downloadView;
	private Handler handler;
	private SeekBar seekBarDownload;
	private int page;
	private ContactServer cont;
	private String fileName;
	private boolean stopDownload;
	private int currentPage;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		handler = new Handler();
		stopDownload = false;
		setContentView(R.layout.downloadfile);
		seekBarDownload = (SeekBar) findViewById(R.id.downloadbar);
		controlBtn = (Button) findViewById(R.id.controldownload);
		downloadView = (TextView) findViewById(R.id.downloadtxt);
		Bundle bundle = getIntent().getExtras();
		fileName = bundle.getString("name");
		List<NameValuePair> dataRequest = new ArrayList<NameValuePair>();
		BasicNameValuePair para = new BasicNameValuePair("name", fileName);
		page = bundle.getInt("page");
		dataRequest.add(para);
		cont = new ContactServer(dataRequest, this);
		downloadView.setText(getString(R.string.clicktodownload)+" "+fileName);
		controlBtn.setOnClickListener(this);
		currentPage = 0;
		downloadFile();
	}
	private void downloadFile(){
		stopDownload = false;
		try {
			controlBtn.setText(getString(R.string.stopBtn));
			downloadView.setText(getString(R.string.downloading)+" "+fileName);
			cont.receiveMultipleFile(new ServiceCallback<Integer[]>() {
				
				@Override
				public void onSuccess(Integer[] result) {
					// TODO Auto-generated method stub
					
					if (result[0] == page-1){
						handler.post(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								controlBtn.setText(getString(R.string.returnBtn));
								downloadView.setText(getString(R.string.finishdownload)+" "+fileName);
							}
						});		
					}
					if (stopDownload){
						currentPage = ++result[0];
						result[0] = -1;
						Log.e("curr", result[0]+"");
					}
				}	
				@Override
				public void onFailure(Exception exception) {
					// TODO Auto-generated method stub
					exception.printStackTrace();
				}
			}, ServerInfo.downloadFile, fileName, page,seekBarDownload,currentPage);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		if (controlBtn.getText().equals(getString(R.string.returnBtn))){
			finish();
		} else if (controlBtn.getText().equals(getString(R.string.stopBtn))){
			stopDownload = true;
			controlBtn.setText(getString(R.string.startBtn));
		} else {
			downloadFile();
		}
	}
	
}
